<?php
/**
 * 活动小程序相关服务类
 * Actutils.php BY My
 * Created by sen at 2021/3/23
 * AgeClub.net @ 2021
 */

namespace app\api\controller\bzh\news;


use app\api\library\Utils;
use app\common\controller\Api;
use think\Db;
use think\Log;
use think\Validate;
use think\Cache;

class News extends Api
{
    protected $noNeedLogin = ['newsList','newsHot','newsDetail','taxonomy','newsListpc'];
    protected $noNeedRight = '*';


    /**
     * 获取展会资讯列表
     */
    public function newsListpc()
    {
        if ($this->request->isGet()) {
            $page = $this->request->request('page', 1);
            $type = $this->request->request('type', 1);
            $total = Db::name('bzh_news')
                ->where('status', 'normal')
                ->where('taxonomy', $type)
                ->count();

            $limit = ($page-1) * 10;
            $list = Db::name('bzh_news')
                ->where('taxonomy', $type)
                ->where('status', 'normal')
                ->limit($limit, 10)
                ->field('id,title,author,createtime,zximage')
                ->select();

            if(empty($list)){
                $this->error('暂无该分页',406);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);
            $this->success('获取信息成功', $result, 200);
        }else{
            $this->error('请求方式有误',305);
        }

    }
    /**
     * 获取展会资讯列表
     */
    public function newsList()
    {
        if ($this->request->isGet()) {
            $page = $this->request->request('page', 1);
            $total = Db::name('bzh_news')
                ->where('status', 'normal')
                ->count();

            $limit = ($page-1) * 5;
            $list = Db::name('bzh_news')
                ->where('status', 'normal')
                ->limit($limit, 5)
                ->field('id,title,author,createtime,zximage')
                ->select();

            if(empty($list)){
                $this->error('暂无该分页',406);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);
            $this->success('获取信息成功', $result, 200);
        }else{
            $this->error('请求方式有误',305);
        }

    }

    /**
     * 获取展会热门资讯列表
     */
    public function newsHot()
    {
        if ($this->request->isGet()) {
            $list = Db::name('bzh_news')
                ->where('status', 'normal')
                ->where('switch', 1)
                ->limit(0, 3)
                ->field('id,title,author,createtime,zximage')
                ->select();

            $result = array("rows" => $list);
            if (!empty($list)) {
                $this->success('获取信息成功', $result, 200);
            } else {
                $this->error('暂未设置热门资讯', 406);
            }
        }

    }


    /**
     * 获取展会热门资讯列表
     */
    public function newsDetail()
    {
        $id=$this->request->request('id','');
        $total = Db::name('bzh_news')
            ->where('id',$id)
            ->count();

        if( $total =='1'){
            $list = Db::name('bzh_news')
                ->where('id',$id)
                ->select();
        }else{
            $this->error('暂无该条资讯','',3010);
        }

        $list = collection($list)->toArray();
        $result = array( "rows" => $list);
        $this->success('获取信息成功', $result, 200);

    }

    /**
 * 获取展会搜索结果
 *
 */
    public function taxonomy()
    {
        if ($this->request->isGet()) {
            $list = Db::name('bzh_taxonomynews')
                ->field('id,taxonomy')
                ->select();
            if(empty($list)){
                $this->error('获取分类失败', '', 3010);
            }
            $list = collection($list)->toArray();
            $result = array("rows" => $list);
            $this->success('获取分类信息成功', $result, 200);
        }
    }


}
